export interface ResolvedFn<T = any> {
  (val: T): T | Promise<T>;
}

export interface RejectedFn {
  (error: any): any;
}

interface Interceptor {
  resolved: ResolvedFn;
  rejected?: RejectedFn;
}

export default class<T> {
  private interceptors: Array<Interceptor>;

  constructor() {
    this.interceptors = [];
  }

  use(resolved: ResolvedFn<T>, rejected?: RejectedFn) {
    this.interceptors.push({
      resolved,
      rejected,
    });
  }

  forEach(fn: (interceptor: Interceptor) => void) {
    this.interceptors.forEach((interceptor) => {
      fn(interceptor);
    });
  }
}
